from sqlalchemy.orm import Session, joinedload

from app.database import engine
from app.schemas.friends import FriendsSM, FriendsStatusSM
from app.models.user import Friend


class FriendsServices:

    @staticmethod
    def store(user_id, form: FriendsSM):
        with Session(engine) as session:
            form_dict = form.dict()
            form_dict['uid'] = user_id
            form_dict['status'] = 2
            obj = Friend(form_dict)
            session.add(obj)
            session.commit()
        return True

    @staticmethod
    def status(friends_id, form: FriendsStatusSM):
        with Session(engine) as session:
            session.query(Friend).filter_by(id=friends_id).update({'status': form.status})
            session.commit()
        return

    @staticmethod
    def get_my_friends(user_id: int):
        with Session(engine) as session:
            user = session.query(Friend).filter_by(uid=user_id).options(joinedload(Friend.friend)).all()
        return user

    @staticmethod
    def get_my_friends_ids(user_id: int):
        with Session(engine) as session:
            friends = session.query(Friend).filter_by(uid=user_id).options(joinedload(Friend.friend)).all()
            id_list = [friend.friend_id for friend in friends]
        return id_list
